package cn.z2huo.knowledge.concurrency.producerconsumer;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <p>
 *
 * @author z2huo
 */
class NotSafePetStore {

    private static final NotSafeDataBuffer<Data> buffer = new NotSafeDataBuffer<>();

    static Callable<Data> produce = () -> {
        Data data = Datas.produceOne();

        try {
            buffer.add(data);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    };

    static Callable<Data> consume = () -> {
        Data data = null;
        try {
            data = buffer.fetch();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    };

    /**
     * <p>线程数量
     */
    private static final int THREAD_TOTAL = 5;

    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_TOTAL);

        for (int i = 0; i < THREAD_TOTAL; i++) {
            threadPool.submit(new Producer(produce, 1500));
            threadPool.submit(new Consumer(consume, 500));
        }

        threadPool.close();
    }

}
